package com.sx.art2.controller;


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import com.sx.art2.beans.Account;
import com.sx.art2.beans.User;
import com.sx.art2.service.AccountService;

@Controller
@SessionAttributes(value={"Account","accountList"}, types= {Account.class,List.class,User.class})
public class AccountController {
	@Autowired
	AccountService accountService;
	@RequestMapping("/accountList")
	public ModelAndView listAdress(HttpSession session) {	
		ModelAndView mv=new ModelAndView();
		User user=(User) session.getAttribute("User");
		List<Account> list =accountService.list(user.getUserId());
		System.out.println(list.size());
		if(!list.isEmpty()) {
			Account li=list.get(0);
			mv.addObject("Account",li);
			mv.addObject("accountList", list);
			mv.setViewName("account");
		}else {
			mv.addObject("First","您的余额已不足,请充值！");
			mv.setViewName("recharge");
		}
		
	
		return mv;		
	}	
	@RequestMapping("/accountShow")
	public ModelAndView accountShow(int userId) {		
		ModelAndView mv=new ModelAndView();	
		
		List<Account> list =accountService.list(userId);		
		System.out.println(list);
		mv.addObject("accountList", list);
		mv.setViewName("admin/account_show");
		return mv;		
	}	
	@RequestMapping("/accountManage")
	public ModelAndView accountManage() {		
		ModelAndView mv=new ModelAndView();	
		
		List<Account> list =accountService.listAll();
		
		System.out.println(list);
		mv.addObject("accountList", list);
		mv.setViewName("admin/Cover_management");
		return mv;		
	}	
	
	
	@RequestMapping("/accountAdd")
	public ModelAndView addAccount(HttpSession session,Account account) {
		
		ModelAndView mv=new ModelAndView();
		User u=(User) session.getAttribute("User");
		Account ac=(Account) session.getAttribute("Account");
		float newBalance;
		if(ac==null) {
			newBalance=account.getAccountTrade();
		}else {
			newBalance=ac.getAccountBalance()+account.getAccountTrade();
		}		
		account.setAccountBalance(newBalance); 
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式	
		account.setAccountDate(df.format(new Date()));
		account.setAccountType("1");
		account.setUserId(u.getUserId());
		accountService.add(account,1);
		mv.setViewName("redirect:/accountList");
		return mv;	
	}
	@RequestMapping("/accountReduce")
	public ModelAndView reAccount(HttpSession session,Account account) {
		
		ModelAndView mv=new ModelAndView();
		
		Account ac=(Account) session.getAttribute("Account");
		float newBalance=ac.getAccountBalance()-account.getAccountTrade();
		if(newBalance>0) {
		account.setAccountBalance(newBalance); 
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式	
		account.setAccountDate(df.format(new Date()));
		account.setAccountType("2");
		account.setUserId(ac.getUserId());
		accountService.add(account,2);
		mv.setViewName("redirect:/accountList");
		}else {
			mv.addObject("message","您的余额已不足，请重新确认金额");
			mv.setViewName("withdraw");
		}
		return mv;	
	}
}
